
<template>
  <a-dropdown
    v-if="currentUser && currentUser.name"
    placement="bottomRight"
  >
    <span class="ant-pro-account-avatar">
      <a-avatar
        size="small"
        :src="avatar"
        class="antd-pro-global-header-index-avatar"
      />
      <span>{{ nickname }}</span>
    </span>
    <template v-slot:overlay>
      <a-menu
        class="ant-pro-drop-down menu"
        :selected-keys="[]"
      >
        <a-menu-item
          v-if="menu"
          key="center"
          @click="handleToCenter"
        >
          <a-icon type="user" />{{ i18nRender('menu.account.center') }}
        </a-menu-item>
        <a-menu-item
          v-if="menu"
          key="settings"
          @click="handleToSettings"
        >
          <a-icon type="setting" />{{ i18nRender('menu.account.center') }}
        </a-menu-item>
        <a-menu-divider v-if="menu" />
        <a-menu-item
          key="logout"
          @click="handleLogout"
        >
          <a-icon type="logout" />{{ i18nRender('logout') }}
        </a-menu-item>
      </a-menu>
    </template>
  </a-dropdown>
  <span v-else>
    <a-spin
      size="small"
      :style="{ marginLeft: 8, marginRight: 8 }"
    />
  </span>
</template>

<script>
// 设置header 中的 语言
import { Modal } from 'ant-design-vue'
import { mapActions, mapGetters } from 'vuex'
import { i18nRender } from '@/locales'
export default {
  name: 'AvatarDropdown',
  props: {
    currentUser: {
      type: Object,
      default: () => null
    },
    menu: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    ...mapGetters(['nickname', 'avatar'])
  },
  methods: {
    ...mapActions(['Logout']),
    handleToCenter () {
      this.$router.push({ path: '/account/center' })
    },
    handleToSettings () {
      this.$router.push({ path: '/account/settings' })
    },
    handleLogout (e) {
      Modal.confirm({
        title: this.$t('layouts.usermenu.dialog.title'),
        content: this.$t('layouts.usermenu.dialog.content'),
        onOk: () => {
          return this.Logout({})
            .then(() => {
              setTimeout(() => {
                window.location.reload()
              }, 16)
            })
            .catch(err => {
              this.$message.error({
                title: '错误',
                description: err.message
              })
            })
        },
        onCancel () { }
      })
    },
    i18nRender (key) {
      return i18nRender(key)
    }
  }
}
</script>

<style lang="less" scoped>
.ant-pro-drop-down {
  /deep/ .action {
    margin-right: 8px;
  }
  /deep/ .ant-dropdown-menu-item {
    min-width: 160px;
  }
}
</style>
